#Boa:Dialog:Dialog1

import wx
import wx.grid
import consultasBD
import Mensaje

def create(parent):
    return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1GRID1, wxID_DIALOG1SALIR, wxID_DIALOG1STATICTEXT1, 
 wxID_DIALOG1STATICTEXT2, 
] = [wx.NewId() for _init_ctrls in range(5)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(147, 185), size=wx.Size(671, 358),
              style=wx.DEFAULT_DIALOG_STYLE, title='Cambios Locales')
        self.SetClientSize(wx.Size(663, 331))

        self.grid1 = wx.grid.Grid(id=wxID_DIALOG1GRID1, name='grid1',
              parent=self, pos=wx.Point(8, 56), size=wx.Size(640, 216),
              style=0)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE,
              self.OnGrid1GridCellChange)

        self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1, label='',
              name='staticText1', parent=self, pos=wx.Point(16, 8),
              size=wx.Size(159, 13), style=0)

        self.staticText2 = wx.StaticText(id=wxID_DIALOG1STATICTEXT2, label='',
              name='staticText2', parent=self, pos=wx.Point(16, 32),
              size=wx.Size(457, 13), style=0)

        self.Salir = wx.Button(id=wxID_DIALOG1SALIR, label='Salir',
              name='Salir', parent=self, pos=wx.Point(536, 288),
              size=wx.Size(115, 23), style=0)
        self.Salir.Bind(wx.EVT_BUTTON, self.OnSalirButton, id=wxID_DIALOG1SALIR)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.padre = parent
        self.sucursal = self.padre.sucursalNro
        self.version_servidor = {}
        
        self.cargaGrilla()        
        
    def __version_servidor(self, tabla):    
        if not self.version_servidor.get(tabla):
            sql = "SELECT COALESCE( max( version ) , 0 ) as max_version FROM cambios_servidor WHERE tabla like '%s'" % tabla
            self.version_servidor[tabla] = consultasBD.ordenCrudaDictRemote(sql)[0]['max_version']
        
        return self.version_servidor[tabla]
        
    def cargaGrilla(self):
        columnas =['Tabla','Hora','Tipo','Version Local','Version Servidor']
        
        self.grid1.CreateGrid(90,len(columnas))
        
        for i, col in enumerate(columnas): 
            self.grid1.SetColLabelValue(i, col)
            
        self.grid1.DisableDragColSize()
        self.grid1.DisableDragRowSize()

        for x in xrange(30):
            for i in xrange(len(columnas)):
                self.grid1.SetReadOnly(x,i,True)
        

        SQL = "SELECT * FROM cambios c JOIN version_tabla vt"

        resultados = consultasBD.ordenCrudaDict(SQL)
               
        for i,r in enumerate(resultados):
                version_servidor = self.__version_servidor(tabla = r["tabla"])
            
                self.grid1.SetCellValue(i, 0, str(r["tabla"])) 
                self.grid1.SetCellValue(i, 1, str(r["hora"])) 
                self.grid1.SetCellValue(i, 2, str(r["tipo"])) 
                self.grid1.SetCellValue(i, 3, str(r["version"]))
                self.grid1.SetCellValue(i, 4, str(version_servidor))
                
        self.grid1.AutoSizeColumn(0,True)
        self.grid1.AutoSizeRow(0,True)
        self.grid1.AutoSizeColumn(1,True)
        self.grid1.AutoSizeColumn(2,True)
        self.grid1.AutoSizeColumn(3,True)
        self.grid1.AutoSizeColumn(4,True)                

    def OnGrid1GridCellChange(self, event):
        event.Skip()

    def OnSalirButton(self, event):
        self.Close()
                
        
